package com.panda.QQserver.service;

import com.panda.qqcommon.Message;
import com.panda.qqcommon.MessageType;
import com.panda.utils.Utility;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;

public class SendMessageToAllService implements Runnable {

    private boolean flag = true;

    @Override
    public void run() {
        //while (flag) {
            System.out.println("请输入想要推送的文件内容：(输入0不再推送)");
            String news = Utility.readString(1000);
//            if(news.equals("0")){
//                flag = false;
//                break;
//            }
            Message message = new Message();
            message.setSender("server");
            message.setContent(news);
            message.setSendTime(new Date().toString());
            message.setMessageType(MessageType.MESSAGE_TO_ALL);
            System.out.println("server pull news to all people");

            HashMap<String, ServerConnectClientThread> hm = ManageClientThreads.getHm();
            Iterator<String> iterator = hm.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                Socket socket = hm.get(key).getSocket();
                ObjectOutputStream objectOutputStream = null;
                try {
                    objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    objectOutputStream.writeObject(message);


                } catch (IOException e) {
                    throw new RuntimeException(e);
                }


            }
//        }
    }


}
